package hh.engine;

import hh.EmptyUtil;
import hh.compile.CompileUtil;
import hh.util.StringUtil;
import hh.visit.TemplateWriter;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class JavaFile {
    
    
    Engine engine;
    String unfinishedSourceCode;
    String sourceCode;
    String templateFile;
    Nodes nodes;
    List<String> imports;
    String pkg;
    public static final String defaultClassNamePrefix = "HHAutoGen";
    Table table;
    
    
    public synchronized TemplateWriter compileAndNew(Table table) {
        this.table = table;
        String className = getClassName();
        
        String code = unfinishedSourceCode
                .replace("{import}", EmptyUtil.generateImportStr(imports))
                .replace("{className}", className)
                .replace("{interfaces}", "extends " + TemplateWriter.class.getName());
        this.sourceCode = code;
        Object o = CompileUtil.compileAndNew(pkg + "." + className, code);
        return (TemplateWriter) o;
    }
    
    public String getClassName() {
        return defaultClassNamePrefix + "_" + table.tableName + "_" + StringUtil.getFileName(templateFile);
    }
}
